package org.hfchunya.mutualHelp.modular.system.service.impl;

import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hfchunya.mutualHelp.core.common.page.LayuiPageFactory;
import org.hfchunya.mutualHelp.core.common.page.LayuiPageInfo;
import org.hfchunya.mutualHelp.modular.system.entity.Categroy;
import org.hfchunya.mutualHelp.modular.system.mapper.CategroyMapper;
import org.hfchunya.mutualHelp.modular.system.model.params.CategroyParam;
import org.hfchunya.mutualHelp.modular.system.model.result.CategroyResult;
import org.hfchunya.mutualHelp.modular.system.service.ICategroyService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 新闻栏目表 服务实现类
 * </p>
 *
 * @author 付道敏
 * @since 2019-05-05
 */
@Service
public class CategroyServiceImpl extends ServiceImpl<CategroyMapper, Categroy> implements ICategroyService {

    @Override
    public void add(CategroyParam param) {
        Categroy entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CategroyParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CategroyParam param) {
        Categroy oldEntity = getOldEntity(param);
        Categroy newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CategroyResult findBySpec(CategroyParam param) {
        return null;
    }

    @Override
    public List<CategroyResult> findListBySpec(CategroyParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CategroyParam param) {
        Page pageContext = getPageContext();
        QueryWrapper<Categroy> objectQueryWrapper = new QueryWrapper<>();
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CategroyParam param) {
        return param.getCategroyId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Categroy getOldEntity(CategroyParam param) {
        return this.getById(getKey(param));
    }

    private Categroy getEntity(CategroyParam param) {
        Categroy entity = new Categroy();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
